

#include "HeaderFiles.h"
#include "comm_struct.h"
#include "uart_comm_hdl.h"


STR_KEY_SCAN  keyHdl[3];


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
FlagStatus key_level_check(ENUM_KEY_NUM num)
{
	FlagStatus key_level;
	
	switch(num)
	{
		case KEY_NUM_1:
			
			key_level =  KEY1_CHECK_READ(); 
	
		break;
		
		case KEY_NUM_2:
		
			key_level =  KEY2_CHECK_READ(); 
		
			break;
		
		case KEY_NUM_3:
			
			key_level =  KEY3_CHECK_READ(); 
			
			break;
		
		default:
			
			break;
	}
	
	return key_level;
}


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
uint8_t key_status_check(STR_KEY_SCAN *keyhdl, ENUM_KEY_NUM num)
{
	
	keyhdl->level = key_level_check(num);
	
	if(keyhdl->level == RESET)
	{
		keyhdl->holdcnt++;
		keyhdl->liftcnt = 0;		
	}
	else
	{
		keyhdl->liftcnt++;
		
		if(keyhdl->liftcnt > KEY_LIFT_CNT_THRE_X2)
		{
			keyhdl->holdcnt = 0;
			keyhdl->liftcnt = KEY_LIFT_CNT_THRE_X2 + 1;
			keyhdl->status = KEY_STATUS_PRESS_NOT;
		}
	}
	
	if(keyhdl->holdcnt > KEY_HODL_CNT_THRE_X2)
	{
		keyhdl->status = KEY_STATUS_PRESS_LONG;
		keyhdl->holdcnt = KEY_HODL_CNT_THRE_X2 + 1;
	}
	
	else if((keyhdl->holdcnt > KEY_HODL_CNT_THRE_X1) && (keyhdl->liftcnt > KEY_LIFT_CNT_THRE))
	{
		keyhdl->status = KEY_STATUS_PRESS_DOWN;
	}
		
	return keyhdl->status;
}


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void key_event_check(void)
{

	userHdl.signal.keystatus[KEY_NUM_1] = key_status_check(&keyHdl[KEY_NUM_1], KEY_NUM_1);
	userHdl.signal.keystatus[KEY_NUM_2] = key_status_check(&keyHdl[KEY_NUM_2], KEY_NUM_2);
	userHdl.signal.keystatus[KEY_NUM_3] = key_status_check(&keyHdl[KEY_NUM_3], KEY_NUM_3);
	
	
	if(userHdl.signal.keystatus[KEY_NUM_1] == KEY_STATUS_PRESS_DOWN)
	{
		userHdl.signal.keyevent = KEY_EVENT_SWEEP_AREA;
	}
	
	else if(userHdl.signal.keystatus[KEY_NUM_2] == KEY_STATUS_PRESS_DOWN)
	{
		userHdl.signal.keyevent = KEY_EVENT_SWEEP;
	}
	
	else if(userHdl.signal.keystatus[KEY_NUM_2] == KEY_STATUS_PRESS_LONG)
	{
		userHdl.signal.keyevent = KEY_EVENT_POWER_OFF;
	}
	
	else if(userHdl.signal.keystatus[KEY_NUM_3] == KEY_STATUS_PRESS_DOWN)
	{
		userHdl.signal.keyevent = KEY_EVENT_RECHARGE;
	}
	
	
	else if((userHdl.signal.keystatus[KEY_NUM_1] == KEY_STATUS_PRESS_LONG) && 
						(userHdl.signal.keystatus[KEY_NUM_3] == KEY_STATUS_PRESS_LONG))
	{
		userHdl.signal.keyevent = KEY_EVENT_NET_SET;	
	}
	
	else
	{
		userHdl.signal.keyevent = 0;
		userHdl.signal.keyevent_old = 0;
	}
	
	
	if(userHdl.signal.keyevent != userHdl.signal.keyevent_old)
	{
		upload_uartcomm_data(ID_KEY_STATUS_UPLOAD);	
	}
	
	userHdl.signal.keyevent_old = userHdl.signal.keyevent;
	
}


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void bumper_status_check(void)
{
	uint8_t bumper_diag_flg[2] = {0,0};
	
	userHdl.signal.bumper[0] = BUMPER1_CHECK_READ();
	userHdl.signal.bumper[1] = BUMPER2_CHECK_READ();
		
	userHdl.signal.bumperstatus = (userHdl.signal.bumper[0] + (userHdl.signal.bumper[1] << 1));
	
	if(userHdl.signal.bumperstatus != userHdl.signal.bumperstatus_old)
	{
		upload_uartcomm_data(ID_BUMPER_UPLOAD);	
	}
	
	userHdl.signal.bumperstatus_old = userHdl.signal.bumperstatus;
	
	#if 0
	userHdl.diag.bumper = !(userHdl.signal.bumper[0] && userHdl.signal.bumper[1]);
	#else
	
	bumper_diag_flg[0] = failing_edge_check(userHdl.signal.bumper[0], userHdl.signal.bumper_old[0]);
	bumper_diag_flg[1] = failing_edge_check(userHdl.signal.bumper[1], userHdl.signal.bumper_old[1]);
	
	if(userHdl.fault.bit.bumper == 0)
	{
		userHdl.diag.bumper      = (bumper_diag_flg[0] || bumper_diag_flg[1]);
		userHdl.fault.bit.bumper = userHdl.diag.bumper;
	}
	
	userHdl.signal.bumper_old[0] = userHdl.signal.bumper[0];
	userHdl.signal.bumper_old[1] = userHdl.signal.bumper[1];
	
	#endif
}


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void wheel_lift_check(void)
{
	userHdl.signal.wheellift[0] = M1_LIFT_CHECK_READ();
	userHdl.signal.wheellift[1] = M2_LIFT_CHECK_READ();
	
	userHdl.diag.wheellift = !(userHdl.signal.wheellift[0] && userHdl.signal.wheellift[1]);
	
	userHdl.signal.wheelliftstatus = (userHdl.signal.wheellift[0] + (userHdl.signal.wheellift[1] << 1));
	
	if(userHdl.signal.wheelliftstatus != userHdl.signal.wheelliftstatus_old)
	{
		upload_uartcomm_data(ID_DROP_STATUS_UPLOAD);
	}
	
	userHdl.signal.wheelliftstatus_old = userHdl.signal.wheelliftstatus;
}


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void charge_status_check(void)
{
	userHdl.signal.chargegpio = CHARGE_STATUS_READ();
		
	#if 0
	if(userHdl.signal.chargegpio == 1)
	{
		userHdl.signal.chargestatus = BAT_STATUS_CHARGE;		
	}
	else
	{
		userHdl.signal.chargestatus = BAT_STATUS_UNCHARGE;
	}
	
	#else
	
	userHdl.signal.chargestatus = 
		battery_status_check(&batCharge, userHdl.signal.real.vdc, userHdl.signal.real.is_dcbus);
	
	#endif
	
	if((userHdl.fault.bit.UVP == 1) || (userHdl.warn.bit.UVP == 1))
	{
		userHdl.signal.chargestatus |= BAT_STATUS_UNDERVOLTAGE;
	}
	
	else
	{
		userHdl.signal.chargestatus &= ~BAT_STATUS_UNDERVOLTAGE;
	}
	
	userHdl.signal.chargegpio_old = userHdl.signal.chargegpio;
}



